//index.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({
    data: {
        navHeight: app.globalData.navHeight, //导航栏高度
    },
    onLoad: function (opt) {},




    getPhone(e) {

        var that = this;
        let errMsg = e.detail.errMsg;
        //加密格式
        let iv = e.detail.iv;
        let encry = e.detail.encryptedData;
        let token = wx.getStorageSync('token');
        let phone = wx.getStorageSync('phone');
        console.log(iv)

        //手机号和验证码验证略

        if (errMsg == 'getPhoneNumber:ok') {
            //发送请求
            wx.request({
                url: rootDocment + '/api/logic/decryptionInformation',
                data: {
                    session_key: wx.getStorageSync('session_key'),
                    iv: iv,
                    encryptedData: encry
                },
                header: {},
                success: function (res3) {

                    console.log(res3.data.msg.result.phoneNumber, 111)
                    var shouji = res3.data.msg.result.phoneNumber

                    that.denglu(shouji)
                    wx.setStorageSync('phone', res3.data.msg.result.phoneNumber);
                    //跳转到其他页面

                }
            })
        } else {
            //跳转到通过短信验证码获取手机号页面
            wx.navigateTo({
                url: '/pages/messageCode/messageCode',
            })
        }


    },

    denglu(shouji) {
        wx.request({
            url: rootDocment + '/api/user/bindMobile',
            data: {
                id: wx.getStorageSync('user_id3'),
                mobile: shouji,
            },
            header: {},
            success: function (res3) {
                if (!res3.data.success) {
                    wx.showToast({
                        title: res3.data.msg,
                        icon: 'none'
                    })
                } else {
                    wx.showToast({
                        title: '绑定成功',
                        icon: 'none'
                    })

                    app.globalData.userInfo.mobile = shouji
                }
                setTimeout(() => {
                    wx.navigateBack();
                }, 2000)
                // console.log(res3, 111)


            }
        })
    },

    //微信授权登录
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log(res.userInfo);
                wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl);
                wx.setStorageSync('nickName', res.userInfo.nickName);
                wx.navigateTo({
                    url: 'shouji/shouji',
                });
            }
        })
    },
    //拒绝
    goBack: function (e) {
        app.gotaburl('index/index');
    },

})